﻿
using System.Collections.Generic;
using System.Linq;
using XQ.RequestOfficeSupplies.DAL;
using XQ.RequestOfficeSupplies.DAL.DAL;
using XQ.RequestOfficeSupplies.DAL.Model;
using System.Drawing;
using System;
using XQ.RequestOfficeSupplies.DAL.Model.Sys;
using XQ.RequestOfficeSupplies.DAL.Models;

namespace XQ.RequestOfficeSupplies.BLL.Sys
{

    public class DepartmentBll
    {
        DepartmentDal dal = new DepartmentDal();

        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <returns></returns>
        public MenuModel GetDepartmentList(string deptName="", TokenModel token=null)
        {
            List<DepartmentModel> departments = dal.GetDepartmentList(deptName, token);
            MenuModel result = new MenuModel();
            result.pId = -1;
            result.title = "";
            List<MenuModel> subItem = new List<MenuModel>();
            result.children = subItem;
            AddChild(result, departments);
            return result;
        }

        /// <summary>
        /// 获取部门列表(手机端)
        /// </summary>
        /// <returns></returns>
        public MenuModel GetDeptList()
        {
            List<DepartmentModel> departments = dal.GetDepartmentList();
            MenuModel result = new MenuModel();
            result.pId = -1;
            result.title = "";
            List<MenuModel> subItem = new List<MenuModel>();
            result.children = subItem;
            AddChildByPhone(result, departments);
            return result;
        }

        /// <summary>
        /// 根据ID获取部门详情
        /// </summary>
        /// <param name="depId"></param>
        /// <returns></returns>
        public Sys_Departments GetDepById(int depId)
        {
            return dal.GetDepById(depId);
        }

        /// <summary>
        /// 递归添加
        /// </summary>
        /// <param name="node"></param>
        private void AddChild(MenuModel node, List<DepartmentModel> nodes)
        {
            List<DepartmentModel> newNdoes;
            bool open = false;
            if (node.pId == -1)
            {
                newNdoes = nodes;
                open = true;
            }
            else { newNdoes = dal.GetDepartmentSubList(node.id); }
            if (newNdoes != null && newNdoes.Count > 0)
            {
                foreach (DepartmentModel _node in newNdoes) //再次遍历，添加子节点
                {
                    MenuModel subNode = new MenuModel();
                    subNode.id = _node.Id;
                    subNode.name = _node.DepartmentName;
                    subNode.code = _node.Code;
                    subNode.title = _node.DepartmentName;
                    subNode.pName = _node.DepartmentPName;
                    subNode.open = open;
                    subNode.depth = _node.depth;
                    subNode.sortNum = _node.Sortnum == null ? 0 : (int)_node.Sortnum;
                    subNode.remark = _node.Remark;
                    subNode.maxSort = _node.MaxSortNum == null ? 0 : (int)_node.MaxSortNum;
                    //subNode.url = _node.Linkurl;
                    subNode.pId = _node.ParentId == null ? 0 : (int)_node.ParentId;
                    subNode.children = new List<MenuModel>();
                    node.children.Add(subNode);
                    //递归
                    AddChild(subNode, nodes);
                }
            }

        }

        /// <summary>
        /// 递归添加(手机端)
        /// </summary>
        /// <param name="node"></param>
        private void AddChildByPhone(MenuModel node, List<DepartmentModel> nodes)
        {
            List<DepartmentModel> newNdoes;
            bool open = true;
            if (node.pId == -1)
            {
                newNdoes = nodes;
                // open = true;
            }
            else { newNdoes = dal.GetDeptSubList(node.id); }
            if (newNdoes != null && newNdoes.Count > 0)
            {
                foreach (DepartmentModel _node in newNdoes) //再次遍历，添加子节点
                {
                    MenuModel subNode = new MenuModel();
                    subNode.id = _node.Id;
                    subNode.name = _node.DepartmentName;
                    subNode.code = _node.Code;
                    subNode.title = _node.DepartmentName;
                    subNode.pName = _node.DepartmentPName;
                    subNode.open = open;
                    subNode.depth = _node.depth;
                    subNode.sortNum = _node.Sortnum == null ? 0 : (int)_node.Sortnum;
                    subNode.remark = _node.Remark;
                    subNode.maxSort = _node.MaxSortNum == null ? 0 : (int)_node.MaxSortNum;
                    //subNode.url = _node.Linkurl;
                    subNode.pId = _node.ParentId == null ? 0 : (int)_node.ParentId;
                    subNode.children = new List<MenuModel>();
                    node.children.Add(subNode);
                    //递归
                    AddChildByPhone(subNode, nodes);
                }
            }

        }
        /// <summary>
        /// 保存部门
        /// </summary>
        /// <param name="dep"></param>
        /// <returns></returns>
        public string SaveDepartment(Sys_Departments dep)
        {
            return dal.SaveDepartment(dep);
        }
        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="id">部门ID</param>
        /// <returns></returns>
        public string DeleteDepartment(int id)
        {
            return dal.DeleteDepartment(id);
        }

        /// <summary>
        /// 删除部门(多删)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteDepartmentByIds(List<int> ids)
        {
            return dal.DeleteDepartmentByIds(ids);
        }

        /// <summary>
        /// 返回部门名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetDepartmentName(int id)
        {
            return dal.GetDepartmentName(id);
        }

        /// <summary>
        ///获取街道下属部门
        /// </summary>
        /// <returns></returns>
        public List<DepartmentModel> GetDepartmentModels()
        {
            int pid = dal.GetDepartmentList().First().Id;
            return dal.GetDepartmentSubList(pid);
        }

        /// <summary>
        ///根据上级部门ID
        /// </summary>
        /// <returns></returns>
        public List<DepartmentModel> GetDepartmentByParentId(int parentId)
        {
            return dal.GetDepartmentSubList(parentId);
        }

        /// <summary>
        /// 获取有部门编号（有边界）的二级部门
        /// </summary>
        /// <returns></returns>
        public List<Sys_Boundary> GetHasCodeDept()
        {
            return dal.GetHasCodeDept();
        }

        /// <summary>
        /// 根据父部门获取子部门的边界
        /// </summary>
        /// <param name="ParentId"></param>
        /// <returns></returns>
        public List<Sys_Boundary> GetDeptByParentId(int ParentId)
        {
            return dal.GetDeptByParentId(ParentId);
        }

        /// <summary>
        /// 根据ID获取部门名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetDeptNameById(int id)
        {
            return dal.GetDepartmentName(id);
        }

        /// <summary>
        /// 根据ID获取部门CODE
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetDeptCodeById(int id)
        {
            return dal.GetDepartmentCode(id);
        }

        public string GetAllDepartmentName(string code)
        {
            return dal.GetAllDepartmentName(code);
        }
        public string GetAllDepartmentName(int depId)
        {
            return dal.GetAllDepartmentName(depId);
        }


        /// <summary>
        /// 检测点在不在多边形之内
        /// </summary>
        /// <param name="curPoint"></param>
        /// <param name="latlngs"></param>
        /// <returns></returns>
        public bool CheckIsIn(Point curPoint, string latlngs)
        {
            System.Drawing.Drawing2D.GraphicsPath gPath = new System.Drawing.Drawing2D.GraphicsPath();
            Region region = new Region();
            gPath.Reset();

            string[] pointArr = latlngs.Split(',');
            List<Point> list = new List<Point>();
            for (int i = 0; i < pointArr.Length; i = i + 2)
            {
                list.Add(new Point((int)(Convert.ToDouble(pointArr[i]) * 1000000), (int)(Convert.ToDouble(pointArr[i + 1]) * 1000000)));
            }
            gPath.AddPolygon(list.ToArray());
            region.MakeEmpty();
            region.Union(gPath);

            //返回判断点是否在多边形里面
            return region.IsVisible(curPoint);
        }

        /// <summary>
        /// 根据坐标返回code
        /// </summary>
        /// <param name="lng"></param>
        /// <param name="lat"></param>
        /// <returns></returns>
        public string GetDeptartmentCode(float lng, float lat)
        {

            int depId = 0;
            Point curPoint = new Point((int)(lng * 1000000), (int)(lat * 1000000));

            List<Sys_Boundary> depts = new DepartmentBll().GetHasCodeDept();
            foreach (Sys_Boundary d in depts)
            {
                if (CheckIsIn(curPoint, d.Latlngs))
                {
                    //社区级部门
                    depId = (int)d.deptpartmentID;
                    break;
                }
            }
            //查找片区是否包含此点
            if (depId > 0)
            {
                int pqDeptId = 0;
                List<Sys_Boundary> list = new DepartmentBll().GetDeptByParentId(depId);
                foreach (Sys_Boundary d in list)
                {
                    if (CheckIsIn(curPoint, d.Latlngs))
                    {
                        //片区级部门
                        pqDeptId = (int)d.deptpartmentID;
                        break;
                    }
                }
                if (pqDeptId > 0)
                {
                    depId = pqDeptId;
                }
            }
            if (depId == 0)
                return "";
            else
                return dal.GetDepById(depId).Code;

        }

        /// <summary>
        /// 根据部门ID查找中心点
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public string GetCenterByDeptID(int deptId)
        {
            return new DepartmentDal().GetCenterByDeptID(deptId);
        }

        /// <summary>
        ///返回所有部门按指定Depth
        /// </summary>
        /// <param name="depth"></param>
        /// <returns></returns>
        public List<Sys_Departments> GetDepartmentsByDepth(int depth)
        {
            return new DepartmentDal().GetDepartmentsByDepth(depth);
        }

        /// <summary>
        /// 获取部门名称
        /// </summary>
        /// <param name="code">部门名称</param>
        /// <returns></returns>
        public string GetDepartmentNameByCode(string code)
        {
            return new DepartmentDal().GetDepartmentNameByCode(code);
        }
        /// <summary>
        /// 获取部门ID
        /// </summary>
        /// <param name="code">部门代码</param>
        /// <returns></returns>
        public int GetDepIdByCode(string code)
        {
            return new DepartmentDal().GetDepIdByCode(code);
        }

        /// <summary>
        /// 获取所有行政村
        /// </summary>
        /// <returns></returns>
        public List<Sys_Departments> GetDepartmentsByCun()
        {
            return new DepartmentDal().GetDepartmentsByCun();
        }
        /// <summary>
        /// 获取所有部门(tree)
        /// </summary>
        /// 2020/04/17
        /// <returns></returns>
        public List<TreeModel> GetBuMenTree()
        {
            DepartmentDal dal = new DepartmentDal();
            return dal.GetTree();
        }

        /// <summary>
        /// 获取部门级联
        /// </summary>
        /// <returns></returns>
        public CascaderModel GetDepCascader()
        {
            List<DepartmentModel> departments = dal.GetDepartmentList();
            CascaderModel result = new CascaderModel();
            result.pId = -1;
            result.title = "";
            List<CascaderModel> subItem = new List<CascaderModel>();
            result.children = subItem;
            AddCascader(result, departments);
            return result;
        }

        /// <summary>
        /// 递归添加
        /// </summary>
        /// <param name="node"></param>
        private void AddCascader(CascaderModel node, List<DepartmentModel> nodes)
        {
            List<DepartmentModel> newNdoes;
            bool open = false;
            if (node.pId == -1)
            {
                newNdoes = nodes;
                open = true;
            }
            else { newNdoes = dal.GetDepartmentSubList(node.value); }
            if (newNdoes != null && newNdoes.Count > 0 && newNdoes.Count <=11)
            {
                foreach (DepartmentModel _node in newNdoes) //再次遍历，添加子节点
                {
                    CascaderModel subNode = new CascaderModel();
                    subNode.value = _node.Id;
                    subNode.label = _node.DepartmentName;
                    subNode.code = _node.Code;
                    subNode.title = _node.DepartmentName;
                    subNode.pName = _node.DepartmentPName;
                    subNode.open = open;
                    subNode.depth = _node.depth;
                    subNode.sortNum = _node.Sortnum == null ? 0 : (int)_node.Sortnum;
                    subNode.remark = _node.Remark;
                    subNode.maxSort = _node.MaxSortNum == null ? 0 : (int)_node.MaxSortNum;
                    //subNode.url = _node.Linkurl;
                    subNode.pId = _node.ParentId == null ? 0 : (int)_node.ParentId;
                    subNode.children = new List<CascaderModel>();
                    node.children.Add(subNode);
                    //递归
                    AddCascader(subNode, nodes);
                }
            }

        }


        /// <summary>
        /// 根据单位性质获取部门
        /// </summary>
        /// <returns></returns>
        public List<Sys_Departments> GetDepartmentByType(int? Id, string deptName = "", TokenModel token = null)
        {
            return new DepartmentDal().GetDepartmentByType(Id, deptName, token);
        }

        /// <summary>
        /// 获取指定部门下面的所有子部分ID
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public List<int> GetAllChildDeptIDs(int deptId)
        {
            return new DepartmentDal().GetAllChildDeptIDs(deptId);
        }

        /// <summary>
        /// 获取指定部门下面的所有子部门
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public List<Sys_Departments> GetAllChildDeptByParentID(int parentID)
        {
            return new DepartmentDal().GetAllChildDeptByParentID(parentID);
        }

        /// <summary>
        /// 获取指定部门下面的所有非社区部门
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public List<Sys_Departments> GetStreetDepartmentByParentID(int parentID)
        {
            return new DepartmentDal().GetStreetDepartmentByParentID(parentID);
        }

        public List<Sys_Departments> GetReceiveDepartments()
        {
            return new DepartmentDal().FindAllModel(d => d.Code.StartsWith("ZJJZF"));
        }
        
    }
}
